<template>
  <div class="ranking-cards-container">
    <!-- 桌面端：并排卡片布局 -->
    <div v-if="!isMobile" class="desktop-layout">
      <!-- 收藏排行 -->
      <n-card :bordered="true" size="small" class="ranking-card">
      <template #header>
        <div class="card-header">
          <n-space align="center" :size="6">
            <n-icon :size="16" color="#f0a020">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24">
                <path fill="currentColor" d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
              </svg>
            </n-icon>
            <n-text strong class="card-title">收藏排行</n-text>
          </n-space>
          <n-badge :value="favoriteItems.length" :max="99" type="warning" />
        </div>
      </template>

      <div class="ranking-content">
        <n-spin :show="loading.favorites">
          <div v-if="favoriteItems.length === 0" class="empty-state">
            <n-empty description="暂无收藏数据" size="small" />
          </div>
          <div v-else class="ranking-list">
            <div
              v-for="(item, index) in favoriteItems.slice(0, 10)"
              :key="item.id"
              class="ranking-item"
              @click="handleItemClick(item.id)"
            >
              <div class="rank-number" :class="getRankClass(index)">
                {{ index + 1 }}
              </div>
              <div class="item-avatar">
                <n-avatar
                  :src="item.image_url"
                  :width="24"
                  :style="{ backgroundColor: getGradeColor(item.grade) }"
                  :fallback-src="'/images/item-placeholder.png'"
                />
              </div>
              <div class="item-info">
                <div
                  class="item-name"
                >
                  {{ item.name }}
                </div>
                <div class="item-meta">
                  <n-icon :size="10" color="#f0a020">
                    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 24 24">
                      <path fill="currentColor" d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                    </svg>
                  </n-icon>
                  <span class="favorite-count">{{ item.favorite_count || 0 }}</span>
                </div>
              </div>
            </div>
          </div>
        </n-spin>
      </div>
    </n-card>

    <!-- 哈夫币上涨排行 -->
    <n-card :bordered="true" size="small" class="ranking-card">
      <template #header>
        <div class="card-header">
          <n-space align="center" :size="6">
            <n-icon :size="16" color="#18a058">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24">
                <path fill="currentColor" d="M16 6l2.29 2.29-4.88 4.88-4-4L2 16.59 3.41 18l6-6 4 4 6.3-6.29L22 12V6z"/>
              </svg>
            </n-icon>
            <n-text strong class="card-title">上涨排行(24小时对比)</n-text>
          </n-space>
          <n-badge :value="priceIncreaseItems.length" :max="99" type="success" />
        </div>
      </template>

      <div class="ranking-content">
        <n-spin :show="loading.priceChanges">
          <div v-if="priceIncreaseItems.length === 0" class="empty-state">
            <n-empty description="暂无上涨数据" size="small" />
          </div>
          <div v-else class="ranking-list">
            <div
              v-for="(item, index) in priceIncreaseItems.slice(0, 10)"
              :key="item.id"
              class="ranking-item"
              @click="handleItemClick(item.id)"
            >
              <div class="rank-number" :class="getRankClass(index)">
                {{ index + 1 }}
              </div>
              <div class="item-avatar">
                <n-avatar
                  :src="item.image_url"
                  :width="24"
                  :style="{ backgroundColor: getGradeColor(item.grade) }"
                  :fallback-src="'/images/item-placeholder.png'"
                />
              </div>
              <div class="item-info">
                <div
                  class="item-name"
                >
                  {{ item.name }}
                </div>
                <div class="item-meta">
                  <span class="price-change increase">
                    +{{ (item.increase_rate * 100).toFixed(1) }}%
                  </span>
                  <span class="current-price">{{ item.current_price.toFixed(2) }}</span>
                </div>
              </div>
            </div>
          </div>
        </n-spin>
      </div>
    </n-card>

    <!-- 哈夫币下跌排行 -->
    <n-card :bordered="true" size="small" class="ranking-card">
      <template #header>
        <div class="card-header">
          <n-space align="center" :size="6">
            <n-icon :size="16" color="#d03050">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24">
                <path fill="currentColor" d="M16 18l2.29-2.29-4.88-4.88-4 4L2 7.41 3.41 6l6 6 4-4 6.3 6.29L22 12v6z"/>
              </svg>
            </n-icon>
            <n-text strong class="card-title">下跌排行(24小时对比)</n-text>
          </n-space>
          <n-badge :value="priceDecreaseItems.length" :max="99" type="error" />
        </div>
      </template>

      <div class="ranking-content">
        <n-spin :show="loading.priceChanges">
          <div v-if="priceDecreaseItems.length === 0" class="empty-state">
            <n-empty description="暂无下跌数据" size="small" />
          </div>
          <div v-else class="ranking-list">
            <div
              v-for="(item, index) in priceDecreaseItems.slice(0, 10)"
              :key="item.id"
              class="ranking-item"
              @click="handleItemClick(item.id)"
            >
              <div class="rank-number" :class="getRankClass(index)">
                {{ index + 1 }}
              </div>
              <div class="item-avatar">
                <n-avatar
                  :src="item.image_url"
                  :width="24"
                  :style="{ backgroundColor: getGradeColor(item.grade)}"
                  :fallback-src="'/images/item-placeholder.png'"
                />
              </div>
              <div class="item-info">
                <div
                  class="item-name"
                >
                  {{ item.name }}
                </div>
                <div class="item-meta">
                  <span class="price-change decrease">
                    -{{ (item.decrease_rate * 100).toFixed(1) }}%
                  </span>
                  <span class="current-price">{{ item.current_price.toFixed(2) }}</span>
                </div>
              </div>
            </div>
          </div>
        </n-spin>
      </div>
    </n-card>
    </div>

    <!-- 移动端：标签页布局 -->
    <div v-else class="mobile-layout">
      <n-tabs type="line" animated placement="top" :tab-style="{ padding: '8px 12px' }" class="mobile-tabs">
        <n-tab-pane name="favorites" tab="收藏排行">
          <div class="mobile-ranking-content">
            <n-spin :show="loading.favorites">
              <div v-if="favoriteItems.length === 0" class="mobile-empty-state">
                <n-empty description="暂无收藏数据" size="small" />
              </div>
              <div v-else class="mobile-ranking-list">
                <div
                  v-for="(item, index) in favoriteItems.slice(0, 10)"
                  :key="item.id"
                  class="mobile-ranking-item"
                  @click="handleItemClick(item.id)"
                >
                  <div class="mobile-rank-number" :class="getRankClass(index)">
                    {{ index + 1 }}
                  </div>
                  <div class="mobile-item-avatar">
                    <n-avatar
                      :src="item.image_url"
                      :width="32"
                      :style="{ backgroundColor: getGradeColor(item.grade) }"
                      :fallback-src="'/images/item-placeholder.png'"
                    />
                  </div>
                  <div class="mobile-item-info">
                    <div class="mobile-item-name">{{ item.name }}</div>
                    <div class="mobile-item-meta">
                      <n-icon :size="12" color="#f0a020">
                        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24">
                          <path fill="currentColor" d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                        </svg>
                      </n-icon>
                      <span class="mobile-favorite-count">{{ item.favorite_count || 0 }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </n-spin>
          </div>
        </n-tab-pane>

        <n-tab-pane name="rising" tab="上涨排行">
          <div class="mobile-ranking-content">
            <n-spin :show="loading.priceChanges">
              <div v-if="priceIncreaseItems.length === 0" class="mobile-empty-state">
                <n-empty description="暂无上涨数据" size="small" />
              </div>
              <div v-else class="mobile-ranking-list">
                <div
                  v-for="(item, index) in priceIncreaseItems.slice(0, 10)"
                  :key="item.id"
                  class="mobile-ranking-item"
                  @click="handleItemClick(item.id)"
                >
                  <div class="mobile-rank-number" :class="getRankClass(index)">
                    {{ index + 1 }}
                  </div>
                  <div class="mobile-item-avatar">
                    <n-avatar
                      :src="item.image_url"
                      :width="32"
                      :style="{ backgroundColor: getGradeColor(item.grade) }"
                      :fallback-src="'/images/item-placeholder.png'"
                    />
                  </div>
                  <div class="mobile-item-info">
                    <div class="mobile-item-name">{{ item.name }}</div>
                    <div class="mobile-item-meta">
                      <span class="mobile-price-change increase">
                        +{{ (item.increase_rate * 100).toFixed(1) }}%
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </n-spin>
          </div>
        </n-tab-pane>

        <n-tab-pane name="falling" tab="下跌排行">
          <div class="mobile-ranking-content">
            <n-spin :show="loading.priceChanges">
              <div v-if="priceDecreaseItems.length === 0" class="mobile-empty-state">
                <n-empty description="暂无下跌数据" size="small" />
              </div>
              <div v-else class="mobile-ranking-list">
                <div
                  v-for="(item, index) in priceDecreaseItems.slice(0, 10)"
                  :key="item.id"
                  class="mobile-ranking-item"
                  @click="handleItemClick(item.id)"
                >
                  <div class="mobile-rank-number" :class="getRankClass(index)">
                    {{ index + 1 }}
                  </div>
                  <div class="mobile-item-avatar">
                    <n-avatar
                      :src="item.image_url"
                      :width="32"
                      :style="{ backgroundColor: getGradeColor(item.grade) }"
                      :fallback-src="'/images/item-placeholder.png'"
                    />
                  </div>
                  <div class="mobile-item-info">
                    <div class="mobile-item-name">{{ item.name }}</div>
                    <div class="mobile-item-meta">
                      <span class="mobile-price-change decrease">
                        -{{ (item.decrease_rate * 100).toFixed(1) }}%
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </n-spin>
          </div>
        </n-tab-pane>
      </n-tabs>
    </div>

    <!-- 物品详情弹窗 -->
    <ItemDetailModal
      v-model:show="showDetailModal"
      :object-id="selectedItemId"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useStatsStore } from '@/stores/stats'
import { storeToRefs } from 'pinia'
import ItemDetailModal from '@/components/ItemDetailModal.vue'

// 移动端检测
const isMobile = ref(false)

// 检测屏幕尺寸
const checkMobile = () => {
  isMobile.value = window.innerWidth <= 768
}

// 监听窗口大小变化
onMounted(() => {
  checkMobile()
  window.addEventListener('resize', checkMobile)
})

onUnmounted(() => {
  window.removeEventListener('resize', checkMobile)
})

const statsStore = useStatsStore()
const {
  favoriteItems,
  priceIncreaseItems,
  priceDecreaseItems,
  loading
} = storeToRefs(statsStore)

// 获取排名样式类
const getRankClass = (index: number) => {
  if (index === 0) return 'rank-first'
  if (index === 1) return 'rank-second'
  if (index === 2) return 'rank-third'
  return 'rank-normal'
}

// 根据等级获取背景颜色
const getGradeColor = (grade?: number) => {
  console.log('getGradeColor called with grade:', grade) // 调试信息
  if (!grade) return 'transparent'

  switch (grade) {
    case 1: return '#9e9e9e' // 灰色 - 普通
    case 2: return '#4caf50' // 绿色 - 优秀
    case 3: return '#2196f3' // 蓝色 - 稀有
    case 4: return '#9c27b0' // 紫色 - 史诗
    case 5: return '#ff9800' // 橙色 - 传说
    case 6: return '#f44336' // 红色 - 神话
    default: return 'transparent'
  }
}

// 详情弹窗相关状态
const showDetailModal = ref(false)
const selectedItemId = ref<number | null>(null)

// 处理物品点击
const handleItemClick = (itemId: number) => {
  console.log('RankingCard: 点击显示物品详情, itemId:', itemId)
  selectedItemId.value = itemId
  showDetailModal.value = true
}
</script>

<style scoped>
.ranking-cards-container {
  width: 100%;
  height: 320px;
  min-height: 320px;
}

/* 桌面端布局 */
.desktop-layout {
  display: flex;
  flex-direction: row;
  gap: 12px;
  height: 100%;
}

/* 移动端布局 */
.mobile-layout {
  height: 100%;
  display: flex;
  flex-direction: column;
  width: 100%;
}

.mobile-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.mobile-tabs :deep(.n-tabs-content) {
  flex: 1;
  overflow: hidden;
}

.ranking-card {
  flex: 1;
  min-height: 0;
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.ranking-card :deep(.n-card__content) {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 13px;
}

.ranking-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  max-height: 260px;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  min-height: 80px;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.ranking-item:hover {
  background-color: var(--n-color-hover);
}

.rank-number {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
  color: white;
  flex-shrink: 0;
}

.rank-first {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
  color: #8b4513;
}

.rank-second {
  background: linear-gradient(135deg, #c0c0c0, #e8e8e8);
  color: #4a4a4a;
}

.rank-third {
  background: linear-gradient(135deg, #cd7f32, #daa520);
  color: white;
}

.rank-normal {
  background-color: var(--n-color-border);
  color: var(--n-text-color);
}

.item-avatar {
  flex-shrink: 0;
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
  padding: 3px 6px;
  border-radius: 4px;
  min-height: 20px;
  display: flex;
  align-items: center;
}

.item-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 1px;
  font-size: 10px;
}

.favorite-count {
  color: var(--n-text-color-disabled);
  font-size: 12px;
}

.price-change {
  font-weight: 600;
  font-size: 12px;
}

.price-change.increase {
  color: var(--n-error-color);
}

.price-change.decrease {
  color: var(--n-success-color);
}

.current-price {
  color: var(--n-text-color-disabled);
  font-size: 9px;
}

/* 移动端专用样式 */
.mobile-ranking-content {
  height: 100%;
  max-height: 240px;
  overflow-y: auto;
  padding: 8px 0;
  display: flex;
  flex-direction: column;
}

.mobile-empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 120px;
}

.mobile-ranking-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mobile-ranking-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  background-color: var(--n-card-color);
  border: 1px solid var(--n-border-color);
}

.mobile-ranking-item:active {
  transform: scale(0.98);
  background-color: var(--n-color-hover);
}

.mobile-rank-number {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  color: white;
  flex-shrink: 0;
}

.mobile-item-avatar {
  flex-shrink: 0;
}

.mobile-item-info {
  flex: 1;
  min-width: 0;
}

.mobile-item-name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mobile-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
}

.mobile-favorite-count {
  color: var(--n-text-color);
  font-weight: 500;
}

.mobile-price-change {
  font-weight: 600;
  font-size: 13px;
}

.mobile-price-change.increase {
  color: var(--n-error-color);
}

.mobile-price-change.decrease {
  color: var(--n-success-color);
}

/* 移动端标签页样式优化 */
.mobile-layout :deep(.n-tabs .n-tabs-nav) {
  padding: 0 8px;
  margin-bottom: 8px;
  flex-shrink: 0;
}

.mobile-layout :deep(.n-tabs .n-tab-pane) {
  padding: 0;
  height: 100%;
  overflow: hidden;
}

.mobile-layout :deep(.n-tabs .n-tabs-tab) {
  font-size: 13px;
  font-weight: 500;
  padding: 6px 12px;
}

.mobile-layout :deep(.n-tabs .n-tabs-wrapper) {
  flex-shrink: 0;
}

.mobile-layout :deep(.n-tabs .n-tabs-content) {
  flex: 1;
  height: 0;
}

/* 桌面端响应式设计 */
@media (min-width: 769px) and (max-width: 1200px) {
  .desktop-layout {
    gap: 8px;
  }

  .card-title {
    font-size: 13px;
  }

  .item-name {
    font-size: 13px;
  }
}

/* 移动端触摸优化 */
@media (hover: none) and (pointer: coarse) {
  .mobile-ranking-item:hover {
    background-color: var(--n-card-color);
  }

  .mobile-ranking-item:active {
    background-color: var(--n-color-hover);
  }
}
</style>
